package com.youhome.service.service.impl;

import com.youhome.common.enums.ZillowTypeEnum;
import com.youhome.common.exception.CustomException;
import com.youhome.service.response.*;
import com.youhome.service.service.DownloadService;
import com.youhome.service.service.ZilLowByMetroService;
import com.youhome.service.utils.ZillowJsonExtractUtils;
import com.youhome.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by chen365 on 2019/04/12.
 */
@Service
public class ZilLowByMetroServiceImpl implements ZilLowByMetroService {

    @Autowired
    private DownloadService downloadService;

    @Override
    public List<TimeValueItemResult> getTimeSeriesByMetro(Integer key, String metro, String state, Integer year) throws CustomException {
        String dir = ZillowTypeEnum.getEnumByKey(key).getValue();
        String filename = StringUtils.replaceBlankAndComma(metro) + "_" + StringUtils.replaceBlankAndComma(state);
        String path = "zillow/" + dir + "/" + filename + ".json";
        String json = downloadService.downloadJsonFromOSS(path);
        return ZillowJsonExtractUtils.dislodgeEmptyItem(ZillowJsonExtractUtils.getSeriesDataByYear(json, year));
    }

    @Override
    public BuyerSellerResult get_Buyer_Seller_Index_Metro(String metro, String state) throws CustomException {
        String dir = ZillowTypeEnum.BuyerSellerIndex_Metro.getValue();
        String filename = StringUtils.replaceBlankAndComma(metro) + "_" + StringUtils.replaceBlankAndComma(state);
        String key = "zillow/" + dir + "/" + filename + ".json";
        String json = downloadService.downloadJsonFromOSS(key);
        return ZillowJsonExtractUtils.extractBuyerSellerResultByJson(json);
    }

    @Override
    public ZhviResult get_Metro_Zhvi_Summary_AllHomes(String metro, String state) throws CustomException {
        String dir = ZillowTypeEnum.Metro_Zhvi_Summary_AllHomes.getValue();
        String filename = StringUtils.replaceBlankAndComma(metro) + "_" + StringUtils.replaceBlankAndComma(state);
        String key = "zillow/" + dir + "/" + filename + ".json";
        String json = downloadService.downloadJsonFromOSS(key);
        return ZillowJsonExtractUtils.extractZhviResultByJson(json);
    }


    @Override
    public ZriResult get_Metro_Zri_AllHomes_Summary(Integer key, String metro, String state) throws CustomException {
        String dir = ZillowTypeEnum.getEnumByKey(key).getValue();
        String filename = StringUtils.replaceBlankAndComma(metro) + "_" + StringUtils.replaceBlankAndComma(state);
        String path = "zillow/" + dir + "/" + filename + ".json";
        String json = downloadService.downloadJsonFromOSS(path);
        return ZillowJsonExtractUtils.extractZriResultByJson(json);
    }

    @Override
    public MarketHealthResult get_Market_Health_Index_Metro(String metro, String state) throws CustomException {
        String dir = ZillowTypeEnum.MarketHealthIndex_Metro.getValue();
        String filename = StringUtils.replaceBlankAndComma(metro) + "_" + StringUtils.replaceBlankAndComma(state);
        String path = "zillow/" + dir + "/" + filename + ".json";
        String json = downloadService.downloadJsonFromOSS(path);
        return ZillowJsonExtractUtils.extractMarketHealthResultByJson(json);
    }


}
